Despite its immense age, size, and population and long record of civilizational accomplishment, 19th-century China was politically, economically, and militarily very weak. From the Chinese point of view, this allowed Western powers to push their country around at will.
The British, frustrated with Chinese trade practices—high tariffs, unwillingness to buy British goods, insistence on being paid only in specie—contrived to sell opium into the Chinese market to offset their trade deficit. Opium was then a highly desired commodity in China, but also illegal. The Chinese emperors and mandarins, being no fools, did not desire a drugged-out population.
But the British wouldn’t stop selling opium to Chinese smugglers. This led to the First Opium War, which China lost. That war was resolved by the 1842 Treaty of Nanking, which, among other provisions, ceded Hong Kong Island to Britain in perpetuity.